/*
 * SiFiSy - A File System Simulator
 * Copyright (C) 2010  SevenSoft
 *
 * This file is part of SiFiSy.
 *
 * SiFiSy is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * SiFiSy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SiFiSy.  If not, see <http://www.gnu.org/licenses/>.
 */

package sifisy.newgui;

import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import sifisy.core.Model;
import sifisy.core.Ntfs;

/**
 * @author Alberto Longato
 * @version %I%, %G%
 * @createData 12/05/2010
 */
public class NtfsSchema extends JPanel {
    private int simId;

    public NtfsSchema(int simId){
        this.simId = simId;
    }

   public void Fill(Graphics g, int x, int y, int lenx, int leny, Color c){
        g.setColor(c);
        g.fillRect(x+1, y+1, lenx-1, leny-1);
        g.setColor(Color.black);
    }

    public int DrawFile(Graphics g, int x, int y, int lenx, int leny, int write, Vector<Integer> File, int start, int lenFile){
        
        int oldLenFile=lenFile;
        if(lenFile>11){
            lenFile=11;
            g.setColor(Color.red);
            g.drawString("rappresentati i primi "+lenFile+" dei "+oldLenFile+" blocchi del record", 100, y+2*leny);
            g.setColor(Color.black);
        }
        Color c;
        int j=start;
        int extension = 0;
        if(start > 0) extension = start/2;
        for(int i=0; i < lenFile; i++){
            g.drawLine(x+3, y-write, x+2*lenx-3, y-write);
            if(i==0){
                g.drawString("Header", x+5, y-write-5);
                g.drawString("Bloks:", x+5, y+leny+write);
            }
            else{
                g.drawString("Run #"+ (i+extension), x+5, y-write-5);
                g.drawString(" "+File.elementAt(j)+"-"+(File.elementAt(j)+File.elementAt(j+1)), x+5, y+leny+write);
            }

            g.drawRect(x, y, 2*lenx, leny);
            c= new Color(0,255,255);
            Fill(g, x, y, lenx, leny, c);
            g.drawString(""+File.elementAt(j), x+2, y+write);
            g.drawString(""+File.elementAt(j+1), x+22, y+write);
            x=x+2*lenx;
            j=j+2;
        }
    return x;
    }

     public void DrawMFT(Graphics g, int x, int y, int lenx, int leny, String filename,  Vector<Integer> File, int start, int lenFile, boolean unused, int block){
        Color c;
        
        Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
        g.setFont(font);
        g.drawString("NTFS", 300, 50);

        c= new Color(255,255,0);
        font = new Font(Font.SANS_SERIF, Font.BOLD, 9);
        g.setFont(font);
        int write= leny/2;

        //MTF Record
        g.drawString("MTF", x, y+20);
        g.drawString("Record", x-5, y+30);
        x=x+30;
        //Record Header
        g.drawRect(x, y, lenx/5, leny);
        c= new Color(255,0,0);
        Fill(g, x, y, lenx/5, leny, c);
        g.drawString("Record", x-20, y-70);
        g.drawString(" header", x-20, y-60);
        g.setColor(Color.black);
        g.drawLine(x+1, y-50, x+1, y-5);
        x=x+lenx/5;
        //Standard Info Header
        g.drawRect(x, y, lenx/5, leny);
        c= new Color(255,255,0);
        Fill(g, x, y, lenx/5, leny, c);
        g.drawString("  Standard ", x-20, y+80);
        g.drawString("info header", x-20, y+90);
        g.setColor(Color.black);
        g.drawLine(x+2, y+leny+5, x+2, y+70);
        x=x+lenx/5;
        //Standard Info
        g.drawRect(x, y, 2*lenx+5, leny);
        c= new Color(0,255,0);
        Fill(g, x, y, 2*lenx+5, leny, c);
        g.drawString("Standard", x+2, y+write);
        g.drawString("   info ", x+2, y+write+10);
        x=x+(2*lenx+5);
        // File name header
        g.drawRect(x, y, lenx/5, leny);
        c= new Color(255,255,0);
        Fill(g, x, y, lenx/5, leny, c);
        g.drawString("File name", x-20, y-70);
        g.drawString("  header ", x-20, y-60);
        g.drawLine(x+1, y-50, x+1, y-5);
        x=x+lenx/5;
        // File Name
        g.drawRect(x, y, 2*lenx+5, leny);
        c= new Color(0,255,0);
        Fill(g, x, y, 2*lenx+5, leny, c);
        g.drawString("File name", x+2, y+write);
        g.drawString(" '"+filename+"'", x+2, y+write+10);
        x=x+(2*lenx+5);
        // Data header
        g.drawRect(x, y, lenx/5, leny);
        c= new Color(255,255,0);
        Fill(g, x, y, lenx/5, leny, c);
        g.drawString("  Data", x-20, y-70);
        g.drawString(" header", x-20, y-60);
        g.drawLine(x+1, y-50, x+1, y-5);
        x=x+lenx/5;
        //puntatori ad altri records (solo nel primo)
        if(start==0 && block>0){
           g.drawRect(x, y, 4*lenx+5, leny);
            c= new Color(125,125,125);
            Fill(g, x, y, 4*lenx+5, leny, c);
            g.drawString("   "+block+" puntatori ", x+2, y+write);
            g.drawString("a rispettivi record", x+2, y+write+10);
            x=x+(4*lenx+5);
        }
        //pezzi di file
        x = DrawFile(g, x, y, lenx, leny, write, File, start, lenFile);
        //unused
        if(!unused){
        g.drawRect(x, y, 2*lenx+5, leny);
        c= new Color(0,125,255);
        Fill(g, x, y, 2*lenx+5, leny, c);
        g.drawString("Unused", x+2, y+write);
        x=x+(2*lenx+5);
        }
     }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int x = 20, y = 100, lenx = 20, leny = 40;
        int start=0;
        boolean unused = true;
        boolean last = false;
        //Inizio File
        String nomefile=""+ Model.getSimulation(simId).getFiles().elementAt(0).getId();
        Vector<Integer> File = ((Ntfs)Model.getSimulation(simId)).getFileRecordsComposition();
/*
        File = new Vector<Integer>();
        File.add(0); File.add(0);
        File.add(1); File.add(1);
        File.add(2); File.add(2);
        File.add(3); File.add(3);
        File.add(4); File.add(4);
        File.add(5); File.add(5);
        File.add(6); File.add(6);
        File.add(7); File.add(7);
      */

        int baseLength = ((Ntfs)Model.getSimulation(simId)).getBaseRecordCouples();
        int extensionLength = ((Ntfs)Model.getSimulation(simId)).getExtensionRecordCouples();
        //Fine file

        if(baseLength > File.size()/2){
            baseLength = File.size()/2;
            unused = false;
        }

        if(baseLength == File.size()/2){
            baseLength = File.size()/2;
        }

        int iteration = 0;
        if(extensionLength > 0){
            iteration = (int)Math.ceil(
                    (
                    (double)File.size() /
                    2 - baseLength
                    ) / (double)(extensionLength) );
        }else{
            iteration = ((File.size()/2-baseLength));
        }

        if (extensionLength >0)
         if((File.size() / 2-baseLength) % extensionLength > 0)
            {iteration++; last=true;}
        //se tolgo spazio dal base per inidirizzare, lo metto nell'extension
 
int plus=0;
        if(baseLength >= iteration){
            plus= iteration;
            baseLength=baseLength-iteration;
        }
        else{
            plus= baseLength;
            baseLength=0;
            unused=true;
        }
if (extensionLength >0){
     plus=(plus/extensionLength);
     if (plus%extensionLength>0)
                plus++;
        iteration=iteration+plus;
 }
 else
    iteration=0;
 
 if(baseLength==0)
     baseLength =File.size()/2;
       
        DrawMFT(g, x, y, lenx, leny, nomefile, File, start, baseLength, unused, iteration);
     
        g.setColor(Color.red);
        if(iteration-1 > 0){
        Font font = new Font(Font.SANS_SERIF, Font.BOLD, 12);
        g.setFont(font);
            g.drawString("Ci sono altri "+(iteration-1)+" blocchi di estensione", x+lenx, 430);
        font = new Font(Font.SANS_SERIF, Font.BOLD, 9);
        g.setFont(font);
            if(last)
                 g.drawString("L'ultimo record e' parzialmente occupato", x+lenx*2, 450);
        }
        g.setColor(Color.black);

        if(extensionLength>0){
            if(extensionLength+baseLength>File.size()/2){
                extensionLength=File.size()/2-baseLength;
                unused=false;
            }
            y=y+200;
            x=20;
            DrawMFT(g, x, y, lenx, leny, nomefile, File, start+(baseLength*2), extensionLength, unused, 0);
        }
    }
    
    /*
    public static void main(String[] args) {
        NtfsSchema disegno = new NtfsSchema(0);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(disegno);
        f.setSize(700, 600);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }*/
    
}